package ecstore.client.customWidgets;

import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.ClickEvent;

import ecstore.client.DataStorageService;
import ecstore.client.DataStorageServiceAsync;
import ecstore.client.dataOperation.ItemInfo;

import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.HasVerticalAlignment;


public class AddItemPanel extends Composite{
	VerticalPanel verticalPanel = new VerticalPanel();
	private final DataStorageServiceAsync dataStorageService = GWT.create(DataStorageService.class);
	private ItemEditorPanel itemEditorPanel = new ItemEditorPanel();
	
	public AddItemPanel() {
		verticalPanel.setSize("628px", "414px");
		
		Label intro = new Label("Add an item to the store");
		verticalPanel.add(intro);
		intro.setWidth("230px");
		
		
		initWidget(verticalPanel);
		verticalPanel.add(itemEditorPanel);
		
		HorizontalPanel horizontalPanel = new HorizontalPanel();
		horizontalPanel.setVerticalAlignment(HasVerticalAlignment.ALIGN_MIDDLE);
		horizontalPanel.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_CENTER);
		verticalPanel.add(horizontalPanel);
		horizontalPanel.setWidth("424px");
		
		Button btnOk = new Button("New button");
		horizontalPanel.add(btnOk);
		// Save the item infomation to the datastore
		btnOk.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				ItemInfo itemInfo = itemEditorPanel.getItemInfo();
				String category = itemInfo.getCategory();
				String brand = itemInfo.getBrand();
				String name = itemInfo.getName();
				float price = itemInfo.getPrice();
				
				String size = itemInfo.getSize();
				String description = itemInfo.getDescription();
				String thumbnail = itemInfo.getThumbnail();
				String image = itemInfo.getImage();
				
				int stock = itemInfo.getStock();
				int recmdStock = itemInfo.getRecmdStock();

				if(category.equalsIgnoreCase("Tobacco") || category.equalsIgnoreCase("Cigarettes")){
					int windowNum = itemInfo.getWindowNum();
					int row = itemInfo.getRowNum();
					int col = itemInfo.getColNum();
					
					dataStorageService.addItemService(category, brand, name, price, size, description,
							thumbnail, image, windowNum, row, col, stock, recmdStock, new AsyncCallback<Boolean>() {
						public void onSuccess(Boolean result){
							if(result == true){
								itemEditorPanel.setName("");
								itemEditorPanel.setItemSize("");
								itemEditorPanel.setDescription("");						
							}else{
								System.out.println("failed  ");
							}
						}

						public void onFailure(Throwable caught) {
							System.out.println("failed to add a new item" + caught.getMessage());
							
						}
					});
					
				}
				
				if(category.equalsIgnoreCase("Others")){
					// invoke server side routine to save the item infomation
					dataStorageService.addItemService(category, brand, name, price, size, description,
							thumbnail, image, stock, recmdStock, new AsyncCallback<Boolean>() {
						public void onSuccess(Boolean result){
							if(result == true){
								// Clear the form when it's successfully processed.
								itemEditorPanel.setName("");
								itemEditorPanel.setItemSize("");
								itemEditorPanel.setDescription("");		
							}else{
								System.out.println("failed  ");
							}
						}
						
						@Override
						public void onFailure(Throwable caught) {
							System.out.println("failed to add a new Other item");
						}

					});
				}

			}
		});
		
		btnOk.setText("Ok");
		
		Button btnCancel = new Button("New button");
		horizontalPanel.add(btnCancel);
		btnCancel.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				System.out.println("cancel");
			}
		});
		
		btnCancel.setText("Cancel");
	}

}
